package org.dentalcard.ui.schedule;

import org.dentalcard.entity.Shift;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.service.impl.ScheduleDaoImpl;
import org.dentalcard.ui.api.tools.DecDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @user: Vladimir Chernishov
 * @date: 21.12.11
 * @time: 10:51
 */
public class AddNewShiftPopup extends DecDialog {

    public AddNewShiftPopup(Frame owner) {
        super(owner, "Add New Shift", true);
        setSize(new Dimension(300, 300));
        setResizable(true);
        center(owner);
        init();
    }

    private void init() {
        JPanel main = new JPanel(new GridBagLayout());
        int rowN = 0;
        main.add(new JLabel("name"), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        final JTextField name = new JTextField("name");
        main.add(name, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        rowN++;
        main.add(new JLabel("from"), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        final JTextField from = new JTextField("9");
        main.add(from, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        rowN++;
        main.add(new JLabel("till"), new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        final JTextField till = new JTextField("14");
        main.add(till, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        rowN++;
        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Shift shift = new Shift();
                    shift.setName(name.getText());
                    shift.setFromTime(Integer.parseInt(from.getText()));
                    shift.setTillTime(Integer.parseInt(till.getText()));
                    PersistenceManager.saveOrUpdate(shift);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(AddNewShiftPopup.this, "Cannot save shift.");
                }
                dispose();
            }
        });
        main.add(ok, new GridBagConstraints(0, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        main.add(cancel, new GridBagConstraints(1, rowN, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));

        
        this.getContentPane().add(main);
    }

}
